package com.esun.payment_api.controller;

import com.esun.common.api.*;
import com.esun.common.utils.BlankUtils;
import com.esun.payment_api.bean.ApprovalNodeBean;
import com.esun.payment_api.bean.InvFormBean;
import com.esun.payment_api.bean.InvProcessBean;
import com.esun.payment_api.dao.InvFormDao;
import com.esun.payment_api.dao.InvFormDetail;
import com.esun.payment_api.domain.*;
import com.esun.payment_api.dto.*;
import com.esun.payment_api.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

import static com.esun.common.utils.JWTUtils.getLoginUser;

/**
 * 发票主表接口
 * Create by benett on 2022-03-09
 */
@RestController
@RequestMapping("/inv")
public class InvFormController {

    @Resource
    private InvFormService invFormService;

    @Resource
    SysDictDataService sysDictDataService;

    @Resource
    SysCustomerService sysCustomerService;

    @Resource
    UserService userService;

    @Resource
    XcxInfoService xcxInfoService;

    @ApiOperation("发票-根据Id返回单个发票")
    @GetMapping("/getById")
    @ResponseBody
    public CommonResult<InvFormBean> getById(@RequestParam Integer id) {
        InvFormBean invForm = invFormService.getById(id);
        return CommonResult.success(invForm);
    }

    //此接口已弃用，发票-根据Id返回发票详情
    @GetMapping("/getDetail")
    @ResponseBody
    public CommonResult<InvFormDetail> getDetail(@RequestParam Integer id) {
        InvFormDetail invFormDetail = invFormService.getDetail(id);
        return CommonResult.success(invFormDetail);
    }

    @ApiOperation("发票-根据Id返回发票详情，包含开票内容、相关合同、相关任务号、往来单位")
    @GetMapping("/getInvDetail")
    @ResponseBody
    public CommonResult<InvFormDetail> getInvDetail(@RequestParam Integer id) {
        if (BlankUtils.isBlank(id)) {
            return CommonResult.failed("发票id不能为空");
        }
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取用户信息");
        }
        //判断当前用户是否有查看权限，1-是
        String dataAuth = invFormService.checkInvDataAuth(id, user.getId(), user.getDeptId());
        if ("1".equals(dataAuth)) {
            InvFormDetail invFormDetail = invFormService.getInvDetail(user, id);
            if (invFormDetail.getTaxRate() != null) {
                if ("0".equals(invFormDetail.getTaxRate().toString())) {
                    invFormDetail.setTaxRateStr(invFormDetail.getTaxRate().toString());
                } else {
                    invFormDetail.setTaxRateStr(invFormDetail.getTaxRate().toString().substring(2));
                }
            }
            //控制不同页面的按钮显示，主要为编辑、保存
            if (user.getId().equals(invFormDetail.getOperatorId())) {
                if ("1".equals(invFormDetail.getFlowStatus()) && "0".equals(invFormDetail.getIsCancelled())) {
                    //显示编辑按钮
                    invFormDetail.setEditable(true);
                    //显示保存
                    invFormDetail.setSaveButton(true);
                    //是否展示审批流程
                    invFormDetail.setIsApprovalProcess(false);
                    //显示提交
                    invFormDetail.setIsSubmit(true);
                } else if ("3".equals(invFormDetail.getFlowStatus()) && "0".equals(invFormDetail.getIsCancelled())) {
                    //显示编辑按钮
                    invFormDetail.setEditable(true);
                    //显示保存
                    invFormDetail.setSaveButton(true);
                    invFormDetail.setIsApprovalProcess(true);
                    //显示再次提交
                    invFormDetail.setIsResubmit(true);
                }
            } else {
                invFormDetail.setEditable(false);
                invFormDetail.setSaveButton(false);
            }
            if (user.getId().equals(invFormDetail.getOperatorId()) &&
                    "0".equals(invFormDetail.getIsCancelled()) &&
                    ("2".equals(invFormDetail.getFlowStatus()) || "4".equals(invFormDetail.getFlowStatus()))) {
                invFormDetail.setCancelButton(true);
            } else {
                invFormDetail.setCancelButton(false);
            }
            return CommonResult.success(invFormDetail);
        } else {
            return CommonResult.failed("没有权限查看此发票详情");
        }

    }

    //此接口已弃用，发票-分页查询发票
    @GetMapping("/getList")
    @ResponseBody
    public CommonResult<CommonPage<InvForm>> getList(InvFormQueryParam queryParam,
                                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<InvForm> list = invFormService.getList(queryParam, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("发票-根据发票类型查询发票")
    @GetMapping("/getListByType")
    @ResponseBody
    public CommonResult<CommonPage<InvFormDao>> getListByType(InvFormQueryParam param,
                                                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {

        InvFormQueryParam queryParam = new InvFormQueryParam();
        queryParam.setCoId(param.getCoId());
        //筛选：部门名称
        if (!BlankUtils.isBlank(param.getDeptName())) {
            queryParam.setDeptName(param.getDeptName());
        }
        //筛选：审批节点
        if (!BlankUtils.isBlank(param.getNodeStr())) {
            queryParam.setNodeStr(param.getNodeStr());
        }
        //筛选：开始时间-结束时间
        if (!BlankUtils.isBlank(param.getStartTimeStr()) && !BlankUtils.isBlank(param.getEndTimeStr())){
            queryParam.setStartTimeStr(param.getStartTimeStr());
            queryParam.setEndTimeStr(param.getEndTimeStr());
        }
        //筛选：业务类型
        if (!BlankUtils.isBlank(param.getBusinessTypeStr())) {
            queryParam.setBusinessTypeStr(param.getBusinessTypeStr());
        }
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取登录用户信息");
        }
        if (BlankUtils.isBlank(user.getId())) {
            return CommonResult.failed("登录人id为空");
        }
        if (BlankUtils.isBlank(user.getDeptId())) {
            CommonResult.failed("登录人部门id为空");
        }
        queryParam.setUserId(user.getId());
        queryParam.setDeptId(user.getDeptId());
        //前后端约定：0-6
        if (!BlankUtils.isBlank(param.getTabStr())) {
            queryParam.setTabStr(param.getTabStr());
            switch (param.getTabStr()) {
                case "0":
                    //全部
                    break;
                case "1":
                    //未提交
                    queryParam.setFlowStatus("1");
                    break;
                case "2":
                    //进行中
                    queryParam.setFlowStatus("2");
                    break;
                case "3":
                    //已通过,已超期的也属于已通过
                    queryParam.setFlowStatus("4");
                    queryParam.setIsCancelled("0");
                    //queryParam.setOvertime("0");//未超期
                    break;
                case "4":
                    //已拒绝
                    queryParam.setFlowStatus("3");
                    queryParam.setIsCancelled("0");
                    break;
                case "5":
                    //已作废
                    queryParam.setFlowStatus("3,4");// in (3,4)
                    queryParam.setIsCancelled("1,2");// in (1,2)
                    break;
                case "6":
                    queryParam.setFlowStatus("4");
                    queryParam.setIsCancelled("0");
                    //已超期  return 1
                    queryParam.setOvertime("1");
                    break;
                default:
                    System.out.println("输入参数有误！");
                    break;
            }
        }
        if (!BlankUtils.isBlank(param.getSearchStr())) {
            queryParam.setSearchStr(param.getSearchStr());
        }
        String sumMoney = invFormService.getSumMoneyBySearch(queryParam);
        BigDecimal totalInvMoney = new BigDecimal(sumMoney);
        List<InvFormDao> list = invFormService.getListByType(queryParam, pageSize, pageNum);
        CommonPage<InvFormDao> invFormDaoCommonPage = CommonPage.restPage(list);
        invFormDaoCommonPage.setTotalInvMoney(totalInvMoney);
        return CommonResult.success(invFormDaoCommonPage);
    }

    @ApiOperation("发票-分页模糊搜索查询发票")
    @GetMapping("/getListByAny")
    @ResponseBody
    public CommonResult<CommonPage<InvForm>> getListByAny(String words,
                                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<InvForm> list = invFormService.getListByAny(words, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("发票-根据Id删除发票")
    @RequestMapping("/deleteById")
    public CommonResult deleteById(Integer id) {
        try {
            int count = invFormService.deleteById(id);
            if (count > 0) {
                return CommonResult.success(count);
            } else {
                return CommonResult.failed("删除失败");
            }
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    //此接口已弃用，发票-删除发票，支持批量
    @PostMapping("/delete")
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Integer> ids) {
        int count = invFormService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("发票-更新/保存")
    @PostMapping("/update")
    @ResponseBody
    public CommonResult update(@RequestBody SaveInvDetailParam param) {
        String msg = "发票修改成功";
        if (param != null) {
            List<SysCustomer> customerList = param.getSysDictCustomer();
            SysCustomer customer = new SysCustomer();

            InvForm invForm = new InvForm();
            invForm.setId(param.getId());
            invForm.setUpdateBy(param.getUpdateBy());
            invForm.setFpType(param.getFpType());
            invForm.setTaxRate(param.getTaxRate());
            invForm.setMoney(param.getMoney());
            invForm.setIsReceived(param.getIsReceived());
            invForm.setReceiveDate(param.getReceiveDate());
            invForm.setReceiveType(param.getReceiveType());
            invForm.setNeedContract(param.getNeedContract());
            invForm.setBusinessType(param.getBusinessType());
            invForm.setRemarks(param.getRemarks());
            invForm.setReceiveId(param.getReceiveId());
            invForm.setDkClaimId(param.getDkClaimId());
            invForm.setIsOfflineClaimed(param.getIsOfflineClaimed());
            invForm.setIs_cpyjf(param.getIs_cpyjf());
            if (param.getNeedContract() == 1) {
                //需要关联合同
                invForm.setNoContractReason("");
            } else {
                //不需要关联合同
                invForm.setNoContractReason(param.getNoContractReason());
            }
            if ("1".equals(param.getIsReceived()) && "1".equals(param.getIsOfflineClaimed())) {
                //是到款 并且 是线下或往年已认领
                invForm.setReceiveId(null);//设置为空
                invForm.setDkClaimId(null);
            }
            if(customerList.size() != 0){
                customer = customerList.get(0);
                if (!BlankUtils.isBlank(param.getCusId())){
                    invForm.setCusId(param.getCusId());
                    customer.setUpdateBy(param.getUpdateBy());
                }
                invForm.setCusAddress(customer.getAddress());
                invForm.setCusItin(customer.getItin());
                invForm.setCusOpeningBank(customer.getOpeningBank());
                invForm.setCusBankAccount(customer.getBankAccount());
            }
            //修改字典表中往来单位对应信息
            if (!BlankUtils.isBlank(invForm.getId())) {
                if (!BlankUtils.isBlank(customer.getId())) {
                    SysCustomer oldCustomer = sysCustomerService.selectById(customer.getId());
                    //至少一个信息不相等，则修改往来单位字典表
                    if ((customer.getAddress() != null && !customer.getAddress().equals(oldCustomer.getAddress())) ||
                        (customer.getItin() != null && !customer.getItin().equals(oldCustomer.getItin())) ||
                        (customer.getOpeningBank() != null && !customer.getOpeningBank().equals(oldCustomer.getOpeningBank()))) {
                        if (sysCustomerService.updateById(customer) == 1) {
                            msg += ",往来单位修改成功";
                        }
                    }
                }
                if (invFormService.update(invForm) == 1) {
                    return CommonResult.success(msg);
                }
            } else {
                return CommonResult.failed("修改失败,发票id为空");
            }
        }
        return CommonResult.failed("保存失败，请检查数据");
    }

    @ApiOperation("发票-新增并查询新发票信息")
    @PostMapping("/insertReturnId")
    @ResponseBody
    public CommonResult insertReturnId(@RequestBody InvForm invForm) {
        if (!BlankUtils.isBlank(invForm.getIsReceived()) && "0".equals(invForm.getIsReceived())){
            invForm.setReceiveType(null);
        }
        int count = invFormService.insertReturnId(invForm);
        if (count > 0) {
            Integer invId = invForm.getId();
            System.out.println("获取主键id：" + invId);
            InvFormBean list = invFormService.getById(invId);
            if (list.getTaxRate() != null) {
                //税率转成字符串
                if ("0".equals(list.getTaxRate().toString())) {
                    list.setTaxRateStr(list.getTaxRate().toString());
                } else {
                    list.setTaxRateStr(list.getTaxRate().toString().substring(2));
                }
            }
            return CommonResult.success(list);
        }
        return CommonResult.failed();
    }

    @ApiOperation("发票-发票类型下拉框，支持增值税率模糊查询")
    @RequestMapping("/getInvTypeList")
    public CommonResult getInvTypeList(Short coId, String word) {
        try {
            List<SysDictData> list = sysDictDataService.selectByExample(coId, "fp_type", "dictValue", word);
            return CommonResult.success(list, "发票类型查询成功");
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("发票-到款银行类型下拉框，支持类型值模糊查询")
    @RequestMapping("/getBankTypeList")
    public CommonResult getBankTypeList(Short coId, String word) {
        try {
            List<SysDictData> list = sysDictDataService.selectByExample(coId, "bank_type", "dictValue", word);
            return CommonResult.success(list, "银行类型查询成功");
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("发票作废-作废原因类型下拉框，支持类型值模糊查询")
    @RequestMapping("/getCancelReasonTypeList")
    public CommonResult getCancelReasonTypeList(Short coId, String word) {
        try {
            List<SysDictData> list = sysDictDataService.selectByExample(coId, "cancel_reason_type", "dictValue", word);
            return CommonResult.success(list, "作废原因类型查询成功");
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("发票-业务类型下拉框，支持类型值模糊查询")
    @RequestMapping("/getBusinessTypeList")
    public CommonResult getBusinessTypeList(Short coId, String word) {
        try {
            List<SysDictData> list = sysDictDataService.selectByExample(coId, "business_type", "dictValue", word);
            return CommonResult.success(list, "业务类型查询成功");
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    //此接口已弃用，新建发票-选择合同后-自动带出往来单位
    @RequestMapping("/getInvCustomer")
    public CommonResult getInvCustomer(Integer invId) {
        if (BlankUtils.isBlank(invId)) {
            return CommonResult.failed("发票ID不能为空");
        }
        List<SysDictCustomer> list = invFormService.getInvCustomer(invId);
        return CommonResult.success(list, "获取往来单位成功");
    }

    @ApiOperation("发票作废-输入作废原因并提交")
    @RequestMapping("/cancelPassedInv")
    public CommonResult cancelPassedInv(@RequestBody PInvCancelParam param) {
        try {
            System.out.println("调用前getCancelId：" + param.getCancelId());
            BigDecimal cancelId = invFormService.cancelPassedInv(param);
            System.out.println("调用后cancelId：" + cancelId);
            return CommonResult.success(cancelId, "发票作废提交成功");
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("发票-获取发票审批流程信息")
    @GetMapping("/getInvProcess")
    public CommonResult getInvProcess(String uuid) {
        if (BlankUtils.isBlank(uuid)) {
            return CommonResult.failed("UUID不能为空");
        }
        List<InvProcessBean> list = invFormService.getInvProcess(uuid);
        if (list.size() == 0) {
            return CommonResult.failed("获取发起人流程失败");
        }
        return CommonResult.success(list, "获取发票流程页面成功");
    }

    @ApiOperation("发票作废-获取发票作废审批流程信息")
    @GetMapping("/getInvCancelProcess")
    public CommonResult getInvCancelProcess(String uuid) {
        if (BlankUtils.isBlank(uuid)) {
            return CommonResult.failed("发票作废UUID不能为空");
        }
        System.out.println("发票作废表uuid：" + uuid);
        List<InvProcessBean> list = invFormService.getInvProcess(uuid);
        if (list.size() == 0) {
            return CommonResult.failed("获取发起人流程失败");
        }
        return CommonResult.success(list, "获取发票作废流程页面成功");
    }

    @ApiOperation("发票-发票提交")
    @RequestMapping("/invSubmitApproval")
    public CommonResult invSubmitApproval(@RequestBody SubmitApprovalParam param) {
        if (param == null) {
            return CommonResult.failed("请检查输入参数");
        }
        if (BlankUtils.isBlank(param.getUuid())) {
            return CommonResult.failed("UUID不能为空");
        }
        if (BlankUtils.isBlank(param.getInvId())) {
            return CommonResult.failed("发票id不能为空");
        } else {
            String businessType = invFormService.getBusinessTypeByInvId(param);
            param.setBusinessType(businessType);
            String check = invFormService.checkInvSubmit(param.getInvId());
            if ("1".equals(check)) {
                invFormService.invSubmitApproval(param);
                if ("1".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)41, param.getInvId());
                } else if ("2".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)161, param.getInvId());
                } else if ("3".equals(param.getBusinessType())) {
                    if (param.getDeptId() != (short)90) {
                        xcxInfoService.getFlowIdAndSend((short)321, param.getInvId());
                    } else {
                        xcxInfoService.getFlowIdAndSend((short)221, param.getInvId());
                    }
                } else if ("4".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)201, param.getInvId());
                } else if ("5".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)241, param.getInvId());
                } else if ("6".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)243, param.getInvId());
                } else if ("7".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)245, param.getInvId());
                } else if ("8".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)247, param.getInvId());
                } else if ("9".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)261, param.getInvId());
                } else if ("10".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)401, param.getInvId());
                } else if ("11".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)403, param.getInvId());
                } else if ("12".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)421, param.getInvId());
                } else if ("13".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)423, param.getInvId());
                }
                return CommonResult.success(check);
            } else {
                return CommonResult.failed(check);
            }
        }
    }

    @ApiOperation("发票-发票被拒再次提交")
    @RequestMapping("/invResubmitApproval")
    public CommonResult invResubmitApproval(@RequestBody SubmitApprovalParam param) {
        if (param == null) {
            return CommonResult.failed("请检查输入参数");
        }
        if (BlankUtils.isBlank(param.getUuid())) {
            return CommonResult.failed("UUID不能为空");
        }
        if (BlankUtils.isBlank(param.getInvId())) {
            return CommonResult.failed("发票id不能为空");
        } else {
            String businessType = invFormService.getBusinessTypeByInvId(param);
            param.setBusinessType(businessType);
            String check = invFormService.checkInvSubmit(param.getInvId());
            if ("1".equals(check)) {
                invFormService.invResubmitApproval(param);
                if ("1".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)41, param.getInvId());
                } else if ("2".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)161, param.getInvId());
                } else if ("3".equals(param.getBusinessType())) {
                    if (param.getDeptId() != (short)90) {
                        xcxInfoService.getFlowIdAndSend((short)321, param.getInvId());
                    } else {
                        xcxInfoService.getFlowIdAndSend((short)221, param.getInvId());
                    }
                } else if ("4".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)201, param.getInvId());
                } else if ("5".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)241, param.getInvId());
                } else if ("6".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)243, param.getInvId());
                } else if ("7".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)245, param.getInvId());
                } else if ("8".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)247, param.getInvId());
                } else if ("9".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)261, param.getInvId());
                } else if ("10".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)401, param.getInvId());
                } else if ("11".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)403, param.getInvId());
                } else if ("12".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)421, param.getInvId());
                } else if ("13".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)423, param.getInvId());
                }
                return CommonResult.success(check);
            } else {
                return CommonResult.failed(check);
            }
        }
    }

    @ApiOperation("发票-审批人同意")
    @RequestMapping("/invProcessApprovalAgree")
    public CommonResult invProcessApprovalAgree(ProcessApprovalParam param) {
        if (param == null) {
            return CommonResult.failed("请检查输入参数");
        }
        if (BlankUtils.isBlank(param.getInvId())) {
            return CommonResult.failed("invId不能为空");
        }
        param.setStatus((short)1);
//        System.out.println("flowId：" + param.getFlowId());
//        System.out.println("flowNotes：" + param.getFlowNotes());
//        System.out.println("userId：" + param.getUserId());
//        System.out.println("invId：" + param.getInvId());
        invFormService.invProcessApproval(param);
        xcxInfoService.sendWxMessage(param.getFlowId());

        return CommonResult.success("审批同意");
    }

    @ApiOperation("发票-审批人拒绝")
    @RequestMapping("/invProcessApprovalReject")
    public CommonResult invProcessApprovalReject(ProcessApprovalParam param) {
        if (param == null) {
            return CommonResult.failed("请检查输入参数");
        }
        if (BlankUtils.isBlank(param.getInvId())) {
            return CommonResult.failed("invId不能为空");
        }
        param.setStatus((short)-1);
//        System.out.println("flowId：" + param.getFlowId());
//        System.out.println("flowNotes：" + param.getFlowNotes());
//        System.out.println("userId：" + param.getUserId());
//        System.out.println("invId：" + param.getInvId());
        invFormService.invProcessApproval(param);
        return CommonResult.success("审批拒绝");
    }

    @ApiOperation("发票作废-审批人通过")
    @RequestMapping("/cancelInvProcessApprovalAgree")
    public CommonResult cancelInvProcessApprovalAgree(ProcessApprovalParam param) {
        if (param == null) {
            return CommonResult.failed("请检查输入参数");
        }
        if (BlankUtils.isBlank(param.getInvId())) {
            return CommonResult.failed("invId不能为空");
        }
        param.setStatus((short)1);
        invFormService.cancelInvProcessApproval(param);

        xcxInfoService.sendWxMessage(param.getFlowId());
        return CommonResult.success("发票作废审批通过");
    }

    @ApiOperation("发票作废-审批人拒绝")
    @RequestMapping("/cancelInvProcessApprovalReject")
    public CommonResult cancelInvProcessApprovalReject(ProcessApprovalParam param) {
        if (param == null) {
            return CommonResult.failed("请检查输入参数");
        }
        if (BlankUtils.isBlank(param.getInvId())) {
            return CommonResult.failed("invId不能为空");
        }
        param.setStatus((short)-1);
        invFormService.cancelInvProcessApproval(param);
        return CommonResult.success("发票作废审批拒绝");
    }

    @ApiOperation("发票-补充信息")
    @PostMapping("/updateById")
    @ResponseBody
    public CommonResult updateById(@RequestBody InvForm record) {
        if (record == null) {
            return CommonResult.failed("请检查参数");
        }
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取登录用户信息");
        }
        if (BlankUtils.isBlank(record.getId())) {
            return CommonResult.failed("发票id不能为空");
        }
        record.setUpdateBy(user.getId());
        int count = invFormService.updateById(record);
        if (count > 0) {
            return CommonResult.success(count, "补充信息成功");
        }
        return CommonResult.failed();
    }


    //发票-筛选条件-部门列表
    @GetMapping("/getDeptList")
    public CommonResult getDeptList(){
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取登录用户信息");
        }
        return userService.getDepartmentList(user.getCoId());
    }

    //发票-筛选条件-审批节点
    @GetMapping("/getApprovalNodeList")
    public CommonResult getApprovalNodeList(){
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取登录用户信息");
        }
        List<ApprovalNodeBean> list = invFormService.getApprovalNodeList(user.getCoId());
        return CommonResult.success(list, "审批节点查询成功");
    }

    @ApiOperation("发票-发票复制")
    @RequestMapping("/invCopy")
    public CommonResult invCopy(@RequestBody PInvCopyParam param) {
        try {
//            System.out.println("调用前getNewInvId：" + param.getNewInvId());
            BigDecimal newInvId = invFormService.invCopy(param);
//            System.out.println("调用后返回newInvId：" + newInvId);
            return CommonResult.success(newInvId, "发票复制成功");
        } catch (Exception e) {
            return CommonResult.failed("发票复制失败,请检查");
        }
    }
}
